using System;
using SystemFrameworks.UserException;
using SystemFrameworks.Util;

namespace SystemFrameworks.Util
{
	/// <summary>
	/// Summary description for ValidateCheck.
	/// </summary>
	public class ValidateCheck
	{
		/// <summary>
		/// Construct function
		/// </summary>
		public ValidateCheck()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		/// <summary>
		///  Check the specified string is null or length =0
		/// </summary>
		/// <param name="strValue">Checked string </param>
		/// <param name="strFieldNm">Field name</param>
		/// <returns>Return the checked result</returns>
		public static string chkRequired(string strValue,string strFieldNm)
		{
			string strErrMsg="";
			if(strValue==null || strValue.Trim().Length==0)	
			{
				DataCheckException exHandle=new DataCheckException(DataCheckException.VALUE_REQUIRED_ERROR,strFieldNm);
				strErrMsg=exHandle.getMessage();
			}
			return strErrMsg;
		}
		/// <summary>
		///   The specified string must be the specified length
		/// </summary>
		/// <param name="strValue">Checked string </param>
		/// <param name="length">Required length</param>
		/// <param name="strFieldNm"></param>
		/// <returns>Return the checked result</returns>

		public static string chkStringLength(string strValue,int length,string strFieldNm)
		{
			string strErrMsg="";
			if(strValue.Trim().Length!=length)	
			{
				DataCheckException exHandle=new DataCheckException(DataCheckException.LENGTH_CHECK_SPECIFIED_ERROR,strFieldNm,length);
				strErrMsg=exHandle.getMessage();
			}
			return strErrMsg;
		}
		/// <summary>
		///   The specified string must be the specified length
		/// </summary>
		/// <param name="strValue">Checked value</param>
		/// <param name="length">Required length</param>
		/// <param name="strFieldNm">Field name in Database</param>
		/// <returns>Return the checked result</returns>

		public static string chkDepartmentId(string strValue,string ParentId,string strFieldNm)
		{
			string strErrMsg="";
			int parentLen=ParentId.Length;
			if(parentLen>0 && strValue.Length>ParentId.Length && !strValue.Substring(0,parentLen).Equals(ParentId) )	
			{
				DataCheckException exHandle=new DataCheckException(DataCheckException.LENGTH_CHECK_DEPARTMENT_ERROR,strFieldNm);
				strErrMsg=exHandle.getMessage();
			}
			else
			{
				if(strValue.Length!=ParentId.Length+2)
				{
					DataCheckException exHandle=new DataCheckException(DataCheckException.LENGTH_CHECK_SPECIFIED_ERROR,strFieldNm,ParentId.Length+2);
					strErrMsg=exHandle.getMessage();			
				}
			}
			return strErrMsg;
		}

		/// <summary>
		/// Check the specified string is letters or numbers.
		/// </summary>
		/// <param name="strValue">Checked value</param>
		/// <param name="strFieldNm">Field name</param>
		/// <returns>Return the checked result</returns>
		public static string  chkValidChar(string strValue,string strFieldNm)
		{
			string strData= "~!@#$^&*();:[]{} ?,|-+_=/.0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        
			string strErrMsg = "";
			//check the specified field's value is zero
			if(strValue ==null || strValue.Trim().Length==0)
			{
				return strErrMsg;
			}
			//trim the space 
			strValue = strValue.Trim();
			for(int i=0 ; i<strValue.Length;i++)
			{
				if(strData.IndexOf(strValue[i])==-1)
				{
					DataCheckException exHandle=new DataCheckException(DataCheckException.LENGTH_CHECK_VALID_CHAR_ERROR,strFieldNm);
					strErrMsg=exHandle.getMessage();
				}
			}
			return strErrMsg;
		}

		/// <summary>
		/// Check the specified string is letters or numbers.
		/// </summary>
		/// <param name="strValue">Checked letters or number string </param>
		/// <param name="strFieldNm">Field name</param>
		/// <returns>Return the checked result</returns>
		public static string  chkLetterOrNumber(string strValue,string strFieldNm)
		{
			string strData= ".0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
        
			string strErrMsg = "";
			//check the specified field's value is zero
			if(strValue ==null || strValue.Trim().Length==0)
			{
				return strErrMsg;
			}
			//trim the space 
			strValue = strValue.Trim();
			for(int i=0 ; i<strValue.Length;i++)
			{
				if(strData.IndexOf(strValue[i])==-1)
				{
					DataCheckException exHandle=new DataCheckException(DataCheckException.NUMBER_LETTER_ERROR,strFieldNm);
					strErrMsg=exHandle.getMessage();
				}
			}
			return strErrMsg;
		}
		/// <summary>
		/// Check the specified string is letters
		/// </summary>
		/// <param name="strValue">Checked letters</param>
		/// <param name="strFieldNm">Field name</param>
		/// <returns>Return the checked result</returns>
		public static string  chkLetter(string strValue,string strFieldNm)
		{
			string strData= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        
			string strErrMsg = "";
			//check the specified field's value is zero
			if(strValue ==null || strValue.Trim().Length==0)
			{
				return strErrMsg;
			}
			//trim the space 
			strValue = strValue.Trim();
			for(int i=0 ; i<strValue.Length;i++)
			{
				if(strData.IndexOf(strValue[i])==-1)
				{
					DataCheckException exHandle=new DataCheckException(DataCheckException.LETTER_ERROR,strFieldNm);
					strErrMsg=exHandle.getMessage();
				}
			}
			return strErrMsg;
		}

		/// <summary>
		/// check the specified string is all numbers
		/// </summary>
		/// <param name="strValue"></param>
		/// <param name="strFieldNm"></param>
		/// <returns></returns>
		public static string  chkNumber(string strValue,string strFieldNm)
		{
			string strData= "0123456789.";
        
			string strErrMsg = "";
			//check the specified field's value is zero
			if(strValue ==null || strValue.Trim().Length==0)
			{
				return strErrMsg;
			}
			//trim the space 
			strValue = strValue.Trim();
			for(int i=0 ; i<strValue.Length;i++)
			{
				if(strData.IndexOf(strValue[i])==-1)
				{
					DataCheckException exHandle=new DataCheckException(DataCheckException.NUMBER_ERROR,strFieldNm);
					strErrMsg=exHandle.getMessage();
				}
			}
			return strErrMsg;
		}

		/// <summary>
		/// Check the specified string is all numbers
		/// </summary>
		/// <param name="strValue">Checked number</param>
		/// <param name="strFieldNm">Field name</param>
		/// <returns>Return the checked result</returns>
		
		public static bool  chkNumber(string strValue)
		{
			string strData= "0123456789.";
        
			bool isNumber = true;	
			//check the specified field's value is zero
			if(strValue ==null || strValue.Trim().Length==0)
			{
				return false;
			}
			//trim the space 
			strValue = strValue.Trim();
			for(int i=0 ; i<strValue.Length;i++)
			{
				if(strData.IndexOf(strValue[i])==-1)
				{
				  isNumber = false;
					break;
				}
			}
			return isNumber;
		}

	/// <summary>
	/// Compare the from date and to date
	/// </summary>
	/// <param name="fromDate">Checked start date</param>
	/// <param name="toDate">Checked end date</param>
	/// <param name="strFieldNm">Field name</param>
	/// <returns>Return the checked result</returns>	
		public static string  chkFromToDate(string fromDate ,string toDate,string strFieldNm)
		{
			string strErrMsg = "";
			//check the specified field's value is zero
			if(fromDate ==null || fromDate.Trim().Length==0 || toDate ==null || toDate.Trim().Length==0)
			{
				return strErrMsg;
			}
			//compare the from date is less than to date
			if(fromDate.CompareTo(toDate)>0)
			{
				DataCheckException exHandle=new DataCheckException(DataCheckException.LENGTH_CHECK_FROM_TO_DATE_ERROR,strFieldNm);
				strErrMsg=exHandle.getMessage();			
			}	
			return strErrMsg;
		}


		/// <summary>
		/// Check the phoone numbers string
		/// </summary>
		/// <param name="strValue">Checked phone string </param>
		/// <param name="strFieldNm">Field name</param>
		/// <returns>Return the checked result</returns>
		public static string  chkPhoneNumber(string strValue,string strFieldNm)
		{
			string strData= "0123456789-()";
        
			string strErrMsg = "";
			//check the specified field's value is zero
			if(strValue ==null || strValue.Trim().Length==0)
			{
				return strErrMsg;
			}
			//trim the space 
			strValue = strValue.Trim();
			for(int i=0 ; i<strValue.Length;i++)
			{
				if(strData.IndexOf(strValue[i])==-1)
				{
					DataCheckException exHandle=new DataCheckException(DataCheckException.LENGTH_CHECK_PHONE_ERROR,strFieldNm);
					strErrMsg=exHandle.getMessage();
				}
			}
			return strErrMsg;
		}
		/// <summary>
		/// Check the mail string
		/// </summary>
		/// <param name="strValue">Checked mail</param>
		/// <param name="strFieldNm">Field name</param>
		/// <returns>Return the checked result</returns>
		public static string  chkMailAddress(string strValue,string strFieldNm)
		{
			string strData= "$^_@.0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        
			string strErrMsg = "";
			//check the specified field's value is zero
			if(strValue ==null || strValue.Trim().Length==0)
			{
				return strErrMsg;
			}
			//trim the space 
			strValue = strValue.Trim();
			for(int i=0 ; i<strValue.Length;i++)
			{
				if(strData.IndexOf(strValue[i])==-1)
				{
					DataCheckException exHandle=new DataCheckException(DataCheckException.LENGTH_CHECK_MAIL_ERROR,strFieldNm);
					strErrMsg=exHandle.getMessage();
					return strErrMsg;
				}
			}
			int len=strValue.Length;
			//check the end is .com or .net and it must comprise @ character
			if(strValue.IndexOf('@')==-1 || len<6)
			{
				DataCheckException exHandle=new DataCheckException(DataCheckException.LENGTH_CHECK_MAIL_ERROR,strFieldNm);
				strErrMsg=exHandle.getMessage();
				return strErrMsg;
			}           	
			string temp=strValue.Substring(len-4,4).ToUpper();			
			if(!(temp.Equals(".COM") ||temp.Equals(".NET"))){
				DataCheckException exHandle=new DataCheckException(DataCheckException.LENGTH_CHECK_MAIL_ERROR,strFieldNm);
				strErrMsg=exHandle.getMessage();							
			}
			return strErrMsg;
		}
	}
}
